//
//  EPMScanViewController.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/27.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMScanViewController.h"
#import "EPMScanCoverView.h"
#import <AVFoundation/AVFoundation.h>


@interface EPMScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (weak,nonatomic) EPMScanCoverView* scanCoverView;
// 上下文
@property (strong, nonatomic)   AVCaptureSession    *seesion;

// 摄像头输入
@property (strong, nonatomic)   AVCaptureInput      *videoInput;

// 识别输出
@property (strong, nonatomic)   AVCaptureMetadataOutput     *output;
@end

@implementation EPMScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"店铺二维码扫描";
    [self setupUI];
    // 创建输入
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
    
    // 创建输出
    // AVCaptureMetadataOutput 专门应用于识别比如二维码识别,条形码识别,人脸识别等等
    self.output = [AVCaptureMetadataOutput new];
    
    
    
    
    // 创建上下文
    self.seesion = [AVCaptureSession new];
    
    // 进行输入和输出关联
    if([self.seesion canAddInput:self.videoInput]) {
        [self.seesion addInput:self.videoInput];
    }
    if([self.seesion canAddOutput:self.output]) {
        [self.seesion addOutput:self.output];
    }
    
    // 设置识别类型 只能卸载添加到session之后
    // QRCode 二维码
    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    // 设置获取数据的回调
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    
    // 制作预览
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.seesion];
    previewLayer.frame = self.view.frame;
    
    [self.view.layer addSublayer:previewLayer];
    
    // 启动session
    [self.seesion startRunning];
}
#pragma mark - 回调 -
// 当识别到结果以后回调
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
        //        NSLog(@"obj:%@",obj.stringValue);
      
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:obj.stringValue] options:@{} completionHandler:^(BOOL success) {
            [self.scanCoverView endScanAnimation];
        }];
    }
    
}


#pragma mark - UI设置 -

- (void)setupUI{
    
    EPMScanCoverView* scanWindow = [[EPMScanCoverView alloc]initWithFrame:self.view.bounds];
    
    scanWindow.backgroundColor = [UIColor clearColor];
    _scanCoverView = scanWindow;
    [self.view addSubview:scanWindow];
    [scanWindow startScanAnimation];
   
}



- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scanCoverView endScanAnimation];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
